package net.lab1024.sa.admin.module.business.tx.service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import cn.dev33.satoken.stp.StpUtil;
import net.lab1024.sa.admin.module.business.order.dao.OrderDao;
import net.lab1024.sa.admin.module.business.order.domain.vo.OrderVO;
import net.lab1024.sa.admin.module.business.tx.dao.TxDao;
import net.lab1024.sa.admin.module.business.tx.domain.entity.TxEntity;
import net.lab1024.sa.admin.module.business.tx.domain.form.TxAddForm;
import net.lab1024.sa.admin.module.business.tx.domain.form.TxQueryForm;
import net.lab1024.sa.admin.module.business.tx.domain.form.TxUpdateForm;
import net.lab1024.sa.admin.module.business.tx.domain.vo.TxVO;
import net.lab1024.sa.admin.module.system.employee.domain.entity.EmployeeEntity;
import net.lab1024.sa.admin.module.system.employee.service.EmployeeService;
import net.lab1024.sa.admin.module.system.login.domain.RequestEmployee;
import net.lab1024.sa.admin.module.system.login.service.LoginService;
import net.lab1024.sa.admin.module.system.type.Dao.TypeDao;
import net.lab1024.sa.admin.module.system.type.domain.TypeEntity;
import net.lab1024.sa.admin.module.system.user.dao.UserDao;
import net.lab1024.sa.admin.module.system.user.domain.Enity.UserEntity;
import net.lab1024.sa.admin.module.system.user.domain.JdyVO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.domain.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 提现申请表 Service
 *
 * @Author syf
 * @Date 2025-05-28 10:25:38
 * @Copyright 畅想科技
 */

@Service
public class TxService {

    @Resource
    private TxDao txDao;
    @Resource
    private TypeDao typeDao;
    @Resource
    private UserDao userDao;
    @Resource
    private OrderDao orderDao;
    @Resource
    private LoginService loginService;
    @Resource
    private EmployeeService employeeService;
    /**
     * 分页查询
     *
     * @param queryForm
     * @return
     */
    public PageResult<TxVO> queryPage(TxQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<TxVO> list = txDao.queryPage(page, queryForm);
        List<TypeEntity> typeList = typeDao.selectByKey("tx_status");
        List<String> txStateNames = typeList.stream().map(TypeEntity::getTypeName).collect(Collectors.toList());
        for (TxVO txVO : list) {
            txVO.setTxStatusName(txStateNames.get(txVO.getTxStatus()));
        }
        PageResult<TxVO> pageResult = SmartPageUtil.convert2PageResult(page, list);
        return pageResult;
    }
    public ResponseDTO<JdyVO> detail(String id) {

        UserEntity userEntity = userDao.selectById(id);
        List<OrderVO> orderVOList = orderDao.queryOrderByJdyId(id);
        JdyVO jdy = SmartBeanUtil.copy(userEntity, JdyVO.class);
        TxQueryForm queryForm = new TxQueryForm();
        queryForm.setUserId(id);
        queryForm.setPageNum(1L);
        queryForm.setPageSize(100L);
        PageResult<TxVO> pageResult = queryPage(queryForm);
        List<TxVO> txVO = pageResult.getList();
        List<OrderVO> orderVOList1 =new ArrayList<>();
        for (OrderVO orderVO : orderVOList){
            if (orderVO.getOrderState() == 7) {
                orderVOList1.add(orderVO);
            }
        }
        List<BigDecimal> TxPriceList = txVO.stream().map(TxVO::getTxAmount).collect(Collectors.toList());
        List<BigDecimal> OrderPriceList = orderVOList.stream().map(OrderVO::getOrderPrice).collect(Collectors.toList());
        Optional<BigDecimal> TxbigDecimal = TxPriceList.stream().reduce(BigDecimal::add);
        Optional<BigDecimal> bigDecimal = OrderPriceList.stream().reduce(BigDecimal::add);
        jdy.setAmount(bigDecimal.get().subtract(TxbigDecimal.get()));
        jdy.setOrderCount((long) orderVOList.size());
        Long a = 100-(long) ((double)orderVOList1.size()/(long)orderVOList.size()*100);
        jdy.setTimeoutRate(a);
        return ResponseDTO.ok(jdy);
    }

    /**
     * 添加
     */
    public ResponseDTO<String> add(TxAddForm addForm) {
        TxEntity txEntity = SmartBeanUtil.copy(addForm, TxEntity.class);
        txEntity.setTxTime(LocalDateTime.now());
        txDao.insert(txEntity);
        return ResponseDTO.ok();
    }

    /**
     * 更新
     *
     * @param updateForm
     * @return
     */
    public ResponseDTO<String> update(TxUpdateForm updateForm) {
        // 获取当前登录用户
        Long requestEmployeeId = loginService.getEmployeeIdByLoginId((String) StpUtil.getLoginId());
        EmployeeEntity employeeEntity = employeeService.getById(requestEmployeeId);
        updateForm.setTxShr(employeeEntity.getLoginName());
        updateForm.setShTime(LocalDateTime.now());
        TxEntity txEntity = SmartBeanUtil.copy(updateForm, TxEntity.class);
        txDao.updateById(txEntity);
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     *
     * @param idList
     * @return
     */
    public ResponseDTO<String> batchDelete(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)){
            return ResponseDTO.ok();
        }

        txDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    public ResponseDTO<String> delete(Long txId) {
        if (null == txId){
            return ResponseDTO.ok();
        }

        txDao.deleteById(txId);
        return ResponseDTO.ok();
    }

    public ResponseDTO<TxVO> userDetail(String userId) {
        TxEntity txEntity = txDao.selectById(userId);
        TxVO txVO = SmartBeanUtil.copy(txEntity, TxVO.class);
        if(txVO!= null){
            if(txVO.getTxStatus() == 0){
                txVO.setTxStatusName("待审核");
            }else if(txVO.getTxStatus() == 1){
                txVO.setTxStatusName("审核通过");
            }else if(txVO.getTxStatus() == 2){
                txVO.setTxStatusName("审核不通过");
//            }else if(txVO.getTxStatus() == 4){
//                txVO.setTxStatusName("已打款");
//            }else if(txVO.getTxStatus() == 5){
//                txVO.setTxStatusName("已取消");
            }
        }
        return ResponseDTO.ok(txVO);
    }
}
